package com.appsindy.common.json;

import com.appsindy.common.util.DateUtils;
import com.thoughtworks.xstream.converters.basic.DateConverter;
import java.sql.Date;

public class AppsindyDateConverter extends DateConverter{
    
    public AppsindyDateConverter() {
    }
    
//    public void marshal(Object object, HierarchicalStreamWriter writer, MarshallingContext context) {
//        Holiday holiday = (Holiday)object;
//        if (holiday.getCurrency() != null) {
//            writer.startNode("currency");
//            context.convertAnother(holiday.getCurrency());
//            writer.endNode();            
//        }
//        if (holiday.getDescription()!= null) {
//            writer.addAttribute("description", holiday.getDescription());
//        }
//        if (holiday.getHdate() != null) {
//            writer.startNode("hdate");
//            context.convertAnother(holiday.getHdate().toString());
//            writer.endNode();                        
//        }
//        //if (holiday.getId() != null) {
//            writer.addAttribute("id", String.valueOf(holiday.getId()));            
//        //}
//    }
//    
//    public Object unmarshal(HierarchicalStreamReader hierarchicalStreamReader, UnmarshallingContext unmarshallingContext) {
//        return null;
//    }
    
    public String toString(Object obj) {
    	String dateStr = null;
    	try{
    		java.util.Date d = null;
    		if(obj instanceof java.sql.Date){
		        Date sqlDate = (Date)obj;
		        d = new java.util.Date(sqlDate.getTime()); 
    		} else if(obj instanceof java.util.Date){
    			d = (java.util.Date)obj;
    		}
    		dateStr = d!=null?DateUtils.getDateStr(d, false):obj.toString();
    	} catch(Exception e){
    		dateStr = "[Unknown Date]";
    		e.printStackTrace();
    	}
        //SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");        
        //String s = formatter.format(d);
       // System.out.println("DATE CONVERTER TO = " + s);
    	//System.out.println("[DATE CONVERTER] Result = " + dateStr);
        return dateStr;
    }
    
    public boolean canConvert(Class type) {
    	return type.equals(java.util.Date.class) || type.equals(Date.class);
    }
    
}
